import javax.microedition.lcdui.*;
import java.io.*;

public class PFont {
	private static Image mFontImages[] = new Image[FontsID.nImgID_FntImagesCount];
	private int nCharGroups[][] = new int[FontsID.nImgID_FntCharGroupsCount][];
	private int nCharGroupIndex = 0;

	/**
	 *
	 */
	public boolean LoadCharset(int n_id) {
		if (mFontImages[n_id] == null) {
			mFontImages[n_id] = Resources.LoadImage("/res/f" + n_id);
			if ( mFontImages[n_id] != null ) {
				try {
					//file format
					/*
                    byte bTracking
                    byte bNumGroups - char-groups count
                    For each char group ( bNumGroups):
					int bFirstSymbolCode - char code for first group symbol
					byte bGroupSize - number of elements in group
					for each group element:
						short nPos - X-coord of symbol
						short nWidth - symbols width
					*/
					DataInputStream dis = new DataInputStream(MTMMidlet.the_class.getResourceAsStream("/res/f" + n_id + ".fdt"));
					byte b_tracking = dis.readByte();
					byte b_groups_cnt = dis.readByte();
					for (byte i=0; i<b_groups_cnt; i++) {
						int n_first_char_code = dis.readInt();
						byte b_group_size = dis.readByte();
						int n_last_char_code = n_first_char_code + b_group_size - 1;

						int n_group_size = (b_group_size << 1) + 4;
						nCharGroups[nCharGroupIndex] = new int[n_group_size];
						nCharGroups[nCharGroupIndex][0] = b_tracking;
						nCharGroups[nCharGroupIndex][1] = n_first_char_code;
						nCharGroups[nCharGroupIndex][2] = n_last_char_code;
						nCharGroups[nCharGroupIndex][3] = n_id;

						for (int j=4; j<n_group_size; j+=2) {
							short n_pos = dis.readShort();
							short n_width = dis.readShort();

							nCharGroups[nCharGroupIndex][j] = n_width;
							nCharGroups[nCharGroupIndex][j+1] = n_pos;
						}
						++nCharGroupIndex;
					}
				}
				catch (Exception ex) {
					// System.out.println("LoadCharset exception - " + ex.toString());
				}
			}
		}
		return true;
	}

	/**
	 *
	 */
	public void drawString(Graphics g, String s, int x, int y, int anchor) {
		if ((anchor & (Graphics.HCENTER)) == Graphics.HCENTER) {
			x -= (stringWidth(s) >> 1);
		}
		if ((anchor & (Graphics.RIGHT)) == Graphics.RIGHT) {
			x -= stringWidth(s);
		}
		if ((anchor & (Graphics.BASELINE)) == Graphics.BASELINE) {
			y -= (getHeight() >> 1);
		}
		if ((anchor & (Graphics.BOTTOM)) == Graphics.BOTTOM) {
			y -= getHeight();
		}
		if ((anchor & (Graphics.VCENTER)) == Graphics.VCENTER) {
			y -= (getHeight() >> 1);
		}
		int xx = x;
		int ww;
		boolean b_do;
		Image img;
		for (int i = 0; i < s.length(); i++) {
			int n_char_code = s.charAt(i);
			b_do = true;
			for (int j = 0; b_do && (j < nCharGroupIndex); j++) {
				int ndx = n_char_code - nCharGroups[j][1];
				if ((ndx >= 0) && (n_char_code <= nCharGroups[j][2])) {
					ndx = (ndx<<1) + 4;
					img = mFontImages[nCharGroups[j][3]];
					ww = nCharGroups[j][ndx];
					ndx++;
					if (MTMCanvas.setClip(g, xx, y, ww, img.getHeight(), false)) {
						g.drawImage(img, xx - nCharGroups[j][ndx], y, Graphics.TOP | Graphics.LEFT);
					}
					//#if IMGGROUP_12x12xLimited || IMGGROUP_12x12
						xx += ww + nCharGroups[j][0] - 1; //zag
					//#else
						xx += ww + nCharGroups[j][0]; //zag
					//#endif
					b_do = false;
				}
			}
		}
	}

	/**
     * font height
	 */
	public int getHeight() {
		return mFontImages[0].getHeight();
	}

	/**
	 * string width
	 */
	public int stringWidth(String s) {
		if (s == null) return 0;
		int width = 0;
		boolean b_do;
		for (int i = 0; i<s.length(); i++) {
			int n_char_code = s.charAt(i);
			b_do = true;
			for (int j = 0; b_do && (j < nCharGroupIndex); j++) {
				int ndx = n_char_code - nCharGroups[j][1];
				if ((ndx >= 0) && (n_char_code <= nCharGroups[j][2])) {
					ndx = (ndx<<1) + 4;
					//#if IMGGROUP_12x12xLimited || IMGGROUP_12x12
						width += nCharGroups[j][ndx] + nCharGroups[j][0] - 1; //zag
					//#else
						width += nCharGroups[j][ndx] + nCharGroups[j][0];
					//#endif
					b_do = false;
				}
			}
		}
		return width;
	}
}
